package gui;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

import LogicSide.Session;

public class Main {

	public static Composite current;
	public static Session session;
	public static Display display;
	public static Shell shell;
	public static boolean inSearch = true;
	public static void main(String[] args) {
		/*
		JDBCConnection con = new JDBCConnection();
		con.openConnection(Tools.getHomeURL(), Tools.getUserName(), Tools.getPassword());
		session = new Session(con, 0);
		*/
		
		display = new Display();
		Shell shell = createMain(display, session);
		
		

		shell.open();
		shell.pack();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}

	public static Shell createMain(final Display display, Session session) {
		Main.session = session;
		Main.display = display;
		final Shell shell = new Shell(display);// , SWT.CLOSE | SWT.TITLE |
												// SWT.MIN);
		Main.shell = shell;
		shell.setText("Music+ Login");
		// shell.setSize(800 , 528); //turns out 800 X 528 is the right size to
		// display the whole bg-picture. weird shit.weird shit indeed
		shell.setMinimumSize(800, 530);

		// Set background to shell
		Image loginBackgroundImage = new Image(display, "images/Background-Flower-140.jpg");
		
		shell.setBackgroundImage(loginBackgroundImage);
		shell.setBackgroundMode(SWT.INHERIT_FORCE);

		GridLayout shellLayout = new GridLayout(2, false);
		shell.setLayout(shellLayout);
		shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		FillLayout menuLayout = new FillLayout(SWT.VERTICAL);
		// GridLayout layout = new GridLayout(1, true);

		// Add menu to shell
		Group menu = new Group(shell, SWT.TOP);
		menu.setLayout(menuLayout); // menu is 1-column-layout
		menu.setLayoutData(getMenuLayoutData());

		// Add buttons to menu
		Button searchButton = new Button(menu, SWT.PUSH);
		searchButton.setText("Search");
		
		Image searchButtonImage = new Image(display, "images/search_icon.png");
		searchButton.setImage(searchButtonImage);
		
		searchButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {

				display.asyncExec(new Runnable() {

					@Override
					public void run() {

						// remove current displaying panel
						current.dispose();
						// update current displaying panel
						current = Search.createSearch(shell, Main.session.getCon().getEmptyTable(
								DBside.JDBCConnection.Table.SONG));
						//needed for it to work
						shell.layout(true, true);
						
						
						
						
					}
				});

			}
		});
		
		Button playNowButton = new Button(menu, SWT.PUSH);
		playNowButton.setText("Play Now");
		
		Image playNowButtonImage = new Image(display, "images/playnow_icon.png");
		playNowButton.setImage(playNowButtonImage);
		
		playNowButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				
						// remove current displaying panel
						current.dispose();
						// update current displaying panel
						ArrayList<String> playnow = new ArrayList<String>();
						playnow.add("master of puppets");
						playnow.add("game of thrones");
						current = PlayNow.createPlayNow(shell,
								playnow);
						//needed for it to work
						shell.layout(true, true);
						
				
						
			}
		});

		Button playlistsButton = new Button(menu, SWT.PUSH);
		playlistsButton.setText("Playlists");

		playlistsButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				
						//remove current displaying panel
						current.dispose();
						//update current displaying panel
						current = Playlist.createPlaylist(shell);
						//needed for it to work
						shell.layout(true, true);
						
				
			}
		});

		Button recommendationButton = new Button(menu, SWT.PUSH);
		recommendationButton.setText("Recommendations");
		recommendationButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				
						//remove current displaying panel
						current.dispose();
						//update current displaying panel
						current = Recommendations.createRecommendations(shell);
						//needed for it to work
						shell.layout(true, true);
						
				
			}
		});

		Button songsButton = new Button(menu, SWT.PUSH);
		songsButton.setText("My songs");
		
		songsButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				
						//remove current displaying panel
						current.dispose();
						//update current displaying panel
						current = MySongs.createMySongs(shell);
						//needed for it to work 
						shell.layout(true, true);
						
				
			}
		});
		
		Button friendsButton = new Button(menu, SWT.PUSH | SWT.FLAT);
		friendsButton.setText("Friends");
		friendsButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				display.asyncExec(new Runnable() {
					
					@Override
					public void run() {
				
						//remove current displaying panel
						current.dispose();
						//update current displaying panel
						current = Friends.createFriends(shell);
						//needed for it to work 
						shell.layout(true, true);
						
				
					}
				});
			}
		});
		
		Button editButton = new Button(menu, SWT.PUSH | SWT.FLAT);
		editButton.setText("Edit");
		editButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				display.asyncExec(new Runnable() {
					
					@Override
					public void run() {
				
						//remove current displaying panel
						current.dispose();
						//update current displaying panel
						current = Edit.createEdit(shell);
						//needed for it to work 
						shell.layout(true, true);
						
				
						
					}
				});
			}
		});
		
		  current = Search.createSearch(shell, Main.session.getCon().getEmptyTable(
					DBside.JDBCConnection.Table.SONG));
		// current = PlayNow.createPlayNow(shell, "master of puppets");
		// current = Playlist.createPlaylist(shell);
		// current  = Recommendations.createRecommendations(shell);
		// current = MySongs.createMySongs(shell);
		// current = Friends.createFriends(shell);
		// current = Edit.createEdit(shell);

		return shell;
	}

	private static GridData getMenuLayoutData() {
		GridData gridData = new GridData(GridData.FILL_VERTICAL);
		return gridData;

	}

}